package com.kedou.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kedou.app.Config;
import com.kedou.app.ProjectDetailActivity;
import com.kedou.app.R;
import com.kedou.domain.ProjectItem;
import com.kedou.net.PostUnFollowProject;
import com.kedou.util.NetworkUtil;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;
import com.kedou.widget.SwitchButton;

public class FollowedProjectAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<ProjectItem> mProjects;

	public FollowedProjectAdapter(Context context,
			ArrayList<ProjectItem> projects) {
		this.mContext = context;
		this.mProjects = projects;
	}

	public void addList(ArrayList<ProjectItem> mProjects) {
		this.mProjects.addAll(mProjects);
		this.notifyDataSetChanged();
	}

	public void removeItem(ProjectItem item) {
		this.mProjects.remove(item);
		this.notifyDataSetChanged();
	}

	public ArrayList<ProjectItem> getList() {
		return this.mProjects;
	}

	@Override
	public int getCount() {
		return mProjects.size();
	}

	@Override
	public Object getItem(int position) {
		return mProjects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ProjectItem projectItem = mProjects.get(position);
		LayoutInflater inflater = LayoutInflater.from(mContext);
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.attention_project_item,
					null);
			holder = new ViewHolder();
			holder.mImgProjectDesc = (ImageView) convertView
					.findViewById(R.id.img_project_desc);
			holder.mTvProjectName = (TextView) convertView
					.findViewById(R.id.tv_project_name);
			holder.mSwitchButton = (SwitchButton) convertView
					.findViewById(R.id.btn_switch);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// TODO 图像未处理
		holder.mTvProjectName.setText(projectItem.getProjectName());
		holder.mSwitchButton.setId(position);
		holder.mSwitchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setUnfollowedStatusfromServer(projectItem);
			}
		});
		return convertView;
	}

	/**
	 * 点击 取消关注项目
	 * 
	 * @param mProjectItem
	 */
	public void setUnfollowedStatusfromServer(final ProjectItem mProjectItem) {
		if (NetworkUtil.isNetworkAvailable(mContext)) {
			new PostUnFollowProject(
					PreferencesUtil.getString(mContext, "token"),
					mProjectItem.getProjectId(), PreferencesUtil.getString(
							mContext, "userId"), Config.SOURCE,
					Util.getImei(mContext),
					new PostUnFollowProject.SuccessCallback() {

						@Override
						public void onSuccess(String msg) {
							removeItem(mProjectItem);
							Toast.makeText(mContext, "取消关注成功",
									Toast.LENGTH_SHORT).show();
						}
					}, new PostUnFollowProject.FailCallback() {

						@Override
						public void onFail(String msg) {
							Toast.makeText(mContext, msg, Toast.LENGTH_SHORT)
									.show();
						}
					}, mContext);
		}

	}

	public class ViewHolder {
		/**
		 * 项目图像
		 */
		public ImageView mImgProjectDesc;
		/**
		 * 项目名称
		 */
		public TextView mTvProjectName;
		/**
		 * 关注状态 是否关注
		 */
		public SwitchButton mSwitchButton;

	}
}
